<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?>
<?php 
	$type = $this->input->post('type') ;
	$key = $this->input->post('key') ;
	$category_id_post = $this->input->post('category_id') ;
	$match_id_post = $this->input->post('match_id') ;
	
	$teamIDFeedDataArray = array();
	foreach($this->team_entity->getArrayListOfTeam() as $item){
		if($item->feed_team_id){
			$teamIDFeedDataArray[] = $item->feed_team_id;
		}
	}
	
	$currentMatchArray = $this->feed->getCurrentMatchInfo($type,$key,$match_id_post);
	
	$err = array();
	if(!in_array($currentMatchArray['localteam_id'], $teamIDFeedDataArray )){
		$err[] = "Please add team: ".$currentMatchArray['localteam_name']." before.<br/>";
	}	
	if(!in_array($currentMatchArray['visitorteam_id'], $teamIDFeedDataArray )){
		$err[] = "Please add team: ".$currentMatchArray['visitorteam_name']." before.<br/>";
	}
	if(!empty($err)){
		echo implode('',$err);
		exit();
	}
	
	$tournamentArray = $this->db->where("(status LIKE 'living' OR status LIKE 'upcoming') AND publish LIKE 'publish' AND is_finish LIKE 'no' ")
					->get(TBL_TOURNAMENT)->result();
	$tournamentOption = array();
	foreach($tournamentArray as $item){
		$tournamentOption[$item->id] = $item->name;
	}	
	
	$HOME_TEAM = clone_( $this->team_entity->init('feed_id',$currentMatchArray['localteam_id']) );
	$AWAY_TEAM = clone_( $this->team_entity->init('feed_id',$currentMatchArray['visitorteam_id']) );
	
	$sportDataArray = $this->sport_entity->getArrayIdAndNameOfSport();
	
	$tmp = explode('.',$currentMatchArray['match_formatted_date']);
	$startTimeString = $tmp[2].'-'.$tmp[1].'-'.$tmp[0]." ".$currentMatchArray['match_time'];
	
	$homeScoreArr = $this->feed->getScoresAttrArray($type,$key,$match_id_post,$team='home');
	$home_score = $homeScoreArr['score'];
	$home_wicket = $homeScoreArr['wicket'];
	$home_over = $homeScoreArr['over'];
	
	$awayScoreArr = $this->feed->getScoresAttrArray($type,$key,$match_id_post,$team='away');
	$away_score = $awayScoreArr['score'];
	$away_wicket = $awayScoreArr['wicket'];
	$away_over = $awayScoreArr['over'];
	
	$categoryAttrArr = $this->feed->getCategoryAttr($type,$key,$category_id_post,$match_id_post);	
	if($categoryAttrArr){
		$cat_name = $categoryAttrArr['name'];
	}
?>

<form action="<?php echo site_url('admin/dataman/feed/match_submit');?>" name="match_submit" id="match_submit" method="post" enctype="multipart/form-data" >
	<input type="hidden" name="feed_type" id="feed_type" value="<?php echo $type;?>" />
	<input type="hidden" name="feed_key" id="feed_key" value="<?php echo $key;?>" />
	<input type="hidden" name="feed_match_id" id="feed_match_id" value="<?php echo $match_id_post;?>" />
	<input type="hidden" name="feed_category_id" id="feed_category_id" value="<?php echo $category_id_post;?>" />
	
	<div class="clrboth"></div>
	<div class="grid2"><span class="bold">Match Title:</span></div>
	<div class="grid6"><input type="text" maxlength="500" id="title" name="title" value="<?php echo $cat_name;?>" class="std-input"/></div>
	<div class="grid2"><span class="bold red hidden" id="title_error">Required field.</span></div>
	<div class="clrboth"></div>
	
	<div class="grid2"><span class="bold">*Tournament:</span></div>
	<div class="grid6"><?php echo form_dropdown( 'tournament_id',
								$options = $tournamentOption,
								$selected = array(),
								"id='tournament_id'"
							);	?></div>
	
	<div class="clrboth"></div>
	
	<div class="grid2"><span class="bold">Home Match:</span></div>
	<div class="grid6">
	    <?php 
			echo $HOME_TEAM->getName();
	    ?>
		<input type="hidden" name="home" id="home" value="<?php echo $HOME_TEAM->getId();?>" />
	</div>
	
	<div class="clrboth"></div>
	<div class="grid2"><span class="bold">Home Score:</span></div>
	<div class="grid6"><input type="text" maxlength="5" id="home_score" name="home_score" value="<?php echo $home_score;?>" class="std-input"/></div>
	<div class="grid2"><span class="bold red hidden" id="home_score_error">Required field.</span></div>
	<div class="clrboth"></div>
	
	<div class="clrboth"></div>
	<div class="grid2"><span class="bold">Home Wickets:</span></div>
	<div class="grid6"><input type="text" maxlength="5" id="home_wickets" name="home_wickets" value="<?php echo $home_wicket;?>" class="std-input"/></div>
	<div class="grid2"><span class="bold red hidden" id="home_wickets_error">Required field.</span></div>
	<div class="clrboth"></div>
	
	<div class="clrboth"></div>
	<div class="grid2"><span class="bold">Home Overs:</span></div>
	<div class="grid6"><input type="text" maxlength="5" id="home_overs" name="home_overs" value="<?php echo $home_over;?>" class="std-input"/></div>
	<div class="grid2"><span class="bold red hidden" id="home_overs_error">Required field.</span></div>
	<div class="clrboth"></div>
<!--	
	<div class="clrboth"></div>
	<div class="grid2"><span class="bold">*Home Runs:</span></div>
	<div class="grid6"><input type="text" maxlength="5" id="home_runs" name="home_runs" value="0" class="std-input"/></div>
	<div class="grid2"><span class="bold red hidden" id="home_runs_error">Required field.</span></div>
	<div class="clrboth"></div>
-->	
	<div class="clrboth"></div>
	<div class="grid2"><span class="bold">Away Match:</span></div>
	<div class="grid6">
		<?php echo $AWAY_TEAM->getName();?>
	</div>	
	 	<input type="hidden" name="away" id="away" value="<?php echo $AWAY_TEAM->getId();?>" />				
	<div class="clrboth"></div>
	
	<div class="clrboth"></div>
	<div class="grid2"><span class="bold">Away Score:</span></div>
	<div class="grid6"><input type="text" maxlength="5" id="away_score" name="away_score" value="<?php echo $away_score;?>" class="std-input"/></div>
	<div class="grid2"><span class="bold red hidden" id="away_score_error">Required field.</span></div>
	<div class="clrboth"></div>
	
	<div class="clrboth"></div>
	<div class="grid2"><span class="bold">Away Wickets:</span></div>
	<div class="grid6"><input type="text" maxlength="5" id="away_wickets" name="away_wickets" value="<?php echo $away_wicket;?>" class="std-input"/></div>
	<div class="grid2"><span class="bold red hidden" id="away_wickets_error">Required field.</span></div>
	<div class="clrboth"></div>
	
	<div class="clrboth"></div>
	<div class="grid2"><span class="bold">Away Overs:</span></div>
	<div class="grid6"><input type="text" maxlength="5" id="away_overs" name="away_overs" value="<?php echo $away_over;?>" class="std-input"/></div>
	<div class="grid2"><span class="bold red hidden" id="away_overs_error">Required field.</span></div>
	<div class="clrboth"></div>
<!--	
	<div class="clrboth"></div>
	<div class="grid2"><span class="bold">*Away Runs:</span></div>
	<div class="grid6"><input type="text" maxlength="5" id="away_runs" name="away_runs" value="0" class="std-input"/></div>
	<div class="grid2"><span class="bold red hidden" id="away_runs_error">Required field.</span></div>
	<div class="clrboth"></div>
-->
	<?php 
		if( $currentMatchArray['comment'] == 'Not started'){
			$currentMatchArray['comment'] = 'Match starts at';
		}
	?>
	<div class="clrboth"></div>
	<div class="grid2"><span class="bold">Result:</span></div>
	<div class="grid6"><input type="text" maxlength="255" id="result" name="result" value="<?php echo $currentMatchArray['comment']."<br/>".$currentMatchArray['match_status'];?>" class="std-input"/></div>
	<div class="grid2"><span class="bold red hidden" id="result_error">Required field.</span></div>
	<div class="clrboth"></div>
	
	<div class="grid2"><span class="bold">*Start time:</span></div>
	<div class="grid6">
		<input type="text" maxlength="255" id="start_time" name="start_time" value="<?php echo $startTimeString;?>" class="std-input"/>
	</div>
	<div class="grid2"><span class="bold red hidden" id="start_time_error">Required field.</span></div>
	 
	<div class="clrboth"></div>
	<div class="grid2"><span class="bold">Sport:</span></div>
	<div class="grid6"><?php echo form_dropdown( 'sport_id',
								$options = $sportDataArray,
								$selected = array(),
								"id='sport_id'"
							);	?></div>	
	<div class="clrboth"></div>
	
	<div class="grid2"><span class="bold">*Convertion rate:</span></div>
	<div class="grid6">
		<input type="text" maxlength="255" id="convertion_rate" name="convertion_rate" value="100:25" /> points:coins
	</div>
	<div class="grid2"><span class="bold red hidden" id="convertion_rate_error">Required field.</span></div>
	<div class="clrboth"></div>
	
	<div class="grid2"><span class="bold">Enable Convertion:</span></div>
	<div class="grid6">
		<input type="checkbox" id="convertion_enable" name="convertion_enable" value="yes" /> 
	</div>
	<div class="grid2"><span class="bold red hidden" id="convertion_rate_error">Required field.</span></div>
	<div class="clrboth"></div>
	
	<div class="grid2"><span class="bold">Use default Events:</span></div>
	<div class="grid6">
		<input type="checkbox" id="use_default_event" name="use_default_event" value="yes" checked="checked" /> 
	</div>
	<div class="clrboth"></div>
	
	<div class="grid2"><span class="bold">Email notify enable:</span></div>
	<div class="grid6">
		<input type="checkbox" id="email_notify_enable" name="email_notify_enable" value="yes" checked="checked" /> 
	</div>
	<div class="clrboth"></div>
	
	<div class="grid2"><span class="bold">SMS notify enable:</span></div>
	<div class="grid6">
		<input type="checkbox" id="sms_notify_enable" name="sms_notify_enable" value="yes" checked="checked" /> 
	</div>
	<div class="clrboth"></div>
	
	<div class="grid2">&nbsp;</div>
	<div class="grid6"><input type="submit" class="button" /> <?php echo loader_image('id="loader_image" class="hidden"');?></div>
	<div class="clrboth"></div>	
	
</form>
